// pages/films/index.js
Page({

	sleep(job,ms) {
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				console.log(job + ' done!')
				resolve()
			}, ms)
		})
	},
	async Demo() {
		await this.sleep('job1',3000) 
		await this.sleep('job2',2000)
		await this.sleep('job3',1000)
	},
	regexTest(){ 
		let s='aaa123 www  bbb456 ccc789  567'
		let r, regex=/([a-zA-Z]+)([0-9]+)/g
		while(r=regex.exec(s)){
			console.log(r) 
		}
	},
	onLoad(e) { 
		this.regexTest()
		let data = wx.getStorageSync('menus') 
		if (data) this.setData(data) 
	},
	async types(e) { 
		let typeIndex = e.target.dataset.index 
		if (typeIndex == null) return 
		let menuIndex = this.data.menuIndex 
		let change = {} 
		let menu = this.data.menus[menuIndex] 
		let type= menu.types[typeIndex] 
		let oldTypeIndex = menu.typeIndex 
		if (oldTypeIndex != null) change[`menus[${menuIndex}].types[${oldTypeIndex}].selected`] = false
		change[`menus[${menuIndex}].types[${typeIndex}].selected`] = true
		change[`menus[${menuIndex}].typeIndex`] = typeIndex
		this.setData(change)
		let app=getApp() 
		let s=await app.request({url: `https://www.gow64.com/by/${type.id}.html`})
		let r, list=[], regex=/<a href="([^"]+)" title="([^"]+)" class="module-item-pic"><img class="lazyload" data-src="([^"]+)"/g
		while(r=regex.exec(s)){ 
			
			list.push({
				url: r[1],
				title: r[2],
				img: r[3],
			})
		}
		console.log(list)
		this.setData({[`menus[${menuIndex}].types[${typeIndex}].list`]:list})
	},
	menus(e) { 
		let menuIndex = e.currentTarget.dataset.index 
		if (menuIndex == 0) {
			return
		}
		let oldIndex = this.data.menuIndex 
		let change = {
			menuIndex
		} 
		change[`menus[${oldIndex}].selected`] = false
		change[`menus[${menuIndex}].selected`] = true
		this.setData(change)
	},

	onUnload() { 
		console.log("onUnload")
		this.save()
	},
	onHide() { 
		console.log("onHide")
		this.save()
	},
	save() { 
		let data = this.data
		delete data.__webviewId__
		wx.setStorageSync('menus', data) 
	},



	data: {
		menuIndex: 2,
		menus: [{
			name: '[刷新]'
		}, {
			name: '[搜索]',
		}, {
			name: '电影',
			selected: true,
			types: [
				{ name: '动作片', id: 14 },
				{ name: '喜剧片', id: 15 },
				{ name: '爱情片', id: 16 },
				{ name: '科幻片', id: 17 },
				{ name: '恐怖片', id: 18 },
				{ name: '剧情片', id: 19 },
				{ name: '战争片', id: 20 },
				{ name: '纪录片', id: 21 },
			]
		}, {
			name: '电视剧',
			types: [
				{ name: '国产剧', id: 5 },
				{ name: '香港剧', id: 6 },
				{ name: '台湾剧', id: 7 },
				{ name: '欧美剧', id: 8 },
				{ name: '韩国剧', id: 9 },
				{ name: '日本剧', id: 10 },
				{ name: '泰国剧', id: 11 },
				{ name: '海外剧', id: 12 },
				{ name: '短剧', id: 13 },
			]
		}, {
			name: '综艺',
			types: [
				{ name: '大陆综艺', id: 22 },
				{ name: '港台综艺', id: 23 },
				{ name: '欧美综艺', id: 24 },
				{ name: '日韩综艺', id: 25 },
			]
		}, {
			name: '动漫',
			types: [
				{ name: '国产动漫', id: 26 },
				{ name: '日韩动漫', id: 27 },
				{ name: '欧美动漫', id: 28 },
				{ name: '海外动漫', id: 29 },
				{ name: '动漫电影', id: 30 },
			]
		}]
	},

})